﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System;
using System.Linq;
using System.Windows;
using System.Reflection;
using System.ComponentModel;

using MissedMemo.MeatAndCheeseDisc.OrderMonitorService.OrderProcessing;
using MissedMemo.MeatAndCheeseDisc.OrderMonitorService;


namespace MissedMemo.MeatAndCheeseDisc.OrderMonitor
{
    public partial class MainWindow : Window
    {
        private OrderMonitorServiceAbstraction _serviceAbstraction = new OrderMonitorServiceAbstraction();


        public MainWindow()
        {
            InitializeComponent();

            // Set window caption equal to assembly info product name...

            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes( typeof( AssemblyProductAttribute ), false );
            if( attributes.Length > 0 )
                Title = ( (AssemblyProductAttribute)attributes[0] ).Product;

            // Handle new pizza order
            _serviceAbstraction.NewPizza += ( sender, args ) => App.PizzaOrders.Add( args.Pizza );

            // Handle order cancellation
            _serviceAbstraction.CancelOrder += ( sender, args ) => App.PizzaOrders.Where( pizza => pizza.NowServingNumber == args.OrderId )
                                                                                  .ToList().ForEach( pizza => pizza.IsCancelled = true );

            // Show/Hide on-screen hint
            App.PizzaOrders.CollectionChanged += ( sender, args ) =>
            {
                if( App.PizzaOrders.Count == 0 )
                    labelHint.Visibility = Visibility.Visible;
                else
                    labelHint.Visibility = Visibility.Hidden;
            };

            Closing += ( sender, args ) => _serviceAbstraction.Close();
        }


        // Delay remote comms connection until AFTER window is first displayed

        protected override void OnContentRendered( EventArgs args )
        {
            base.OnContentRendered( args );

            if( false == DesignerProperties.GetIsInDesignMode( this ) )
            {
                try
                {
                    _serviceAbstraction.Connect();
                }
                catch( System.ServiceModel.EndpointNotFoundException e )
                {
                    string strMsg = string.Format( "Make sure the service host is running before starting the client.\n\n{0}", e.Message );
                    MessageBox.Show( strMsg, "Service not found" );

                    App.Current.Shutdown();
                }
            }
        }

        
        private void OnOrderItemCompleted( object sender, System.Windows.Input.ExecutedRoutedEventArgs e )
        {
            Pizza pizza = ( (FrameworkElement)e.OriginalSource ).DataContext as Pizza;

            if( pizza != null )
            {
                if( !pizza.IsCancelled )
                    _serviceAbstraction.NotifyPizzaIsDone( pizza );
                
                App.PizzaOrders.Remove( pizza );
            }
        }
    }
}
